import logging

import pytest

from resource.pom.settings import Settings
from resource.pom.system_ui import SystemUI
from utils.manager import Direction, Images


class Test:
    app = Settings
    system_ui = SystemUI

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.进入桌面，点击设置-WLAN，开启WLAN按钮')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.wait(1)
        device.dirc_fling(Direction.Up)
        device.click(*self.app.wlan_item)
        device.wait(3)
        device.refresh_layout()
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
        logging.info('预期结果1.WLAN按钮被点亮，展示可用WLAN列表')
        device.wait(20)
        switch_area = device.get_element_area(toggle)
        switch_pic = device.save_snapshot_to_local('wifi_switch.jpeg')
        Images.crop_picture(switch_pic, *switch_area)
        similarity = Images.compare_image_similarity(switch_pic, self.app.switch_on)
        assert similarity > 0.8, 'wifi开关未点亮'
        device.refresh_layout()
        device.assert_text_exist('可用WLAN')
        device.assert_text_exist(self.app.open_wifi)

        logging.info('步骤2.选择公开wifi，点击进入，点击删除按钮')
        target_wifi = device.get_element_by_text(self.app.open_wifi)
        device.click_element(target_wifi)
        device.wait(8)
        device.refresh_layout()
        device.assert_text_exist('已连接WLAN')
        device.assert_text_exist(self.app.open_wifi)
        device.click_element(device.get_element_by_text(self.app.open_wifi))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('删除'))
        logging.info('预期结果2.删除后此wifi名称不会再展示在已连接的wifi列表中')
        device.wait(5)
        device.refresh_layout()
        device.assert_text_exist('已连接WLAN', expect=False)
        _, available_y = device.center_of_element(device.get_element_by_text('可用WLAN'))
        open_wifi = device.get_element_by_text(self.app.open_wifi)
        if open_wifi:
            _, open_y = device.center_of_element(open_wifi)
            assert available_y < open_y, '此wifi名称不在可用wlan列表中'
